package com.littlebee.storysuper.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.common.sharePrefUtils.SharePrefUtil;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.Constant;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.ToastUtil;
import com.littlebee.storysuper.utils.Utils;
import com.zss.library.activity.BaseActivity;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.utils.LogUtils;
import com.zss.library.widget.CommonWhiteDialog;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.api.BasicCallback;

/***
 * 绑定设备
 */
public class BindActivity extends BaseActivity {
    EditText et_username;
    Button btn_submit;
    SharedPreferences sharedPreferences;
    TextView toast_tv;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_bind;
    }

    @Override
    public void initView() {
        super.initView();
        sharedPreferences = getSharedPreferences(Constant.appName, Context.MODE_PRIVATE); //私有数据
        toast_tv = (TextView) findViewById(R.id.toast_tv);
        et_username = (EditText) findViewById(R.id.et_username);
        String bindId = SharePrefUtil.getInstance(this).getStringValue(SharePrefUtil.HASBINDDEVICEID);
        if (TextUtils.isEmpty(bindId)) {
            toast_tv.setText(getString(R.string.wait_bind_txt));
        } else {
            toast_tv.setText(getString(R.string.end_bind_txt));
            et_username.setText(bindId);
        }
        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_username.getText().toString().trim())) {
                    ToastUtil.displayShortToast(R.string.please_driver_txt);

                    return;
                }
                loadData(et_username.getText().toString().trim());
            }
        });
    }

    /**
     * 绑定设备
     */
    public void loadData(final String device) {
        Map<String, String> hashParams = new HashMap<>();
        User user = Utils.getUserEntry();
        hashParams.put("phoneId", user.getMobile());
        hashParams.put("deviceid", device);
        // TODO: 2017/8/14
        NetWorkUtils.post(getActivity(), "binddevice", hashParams, true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                LogUtils.i("XHX", "XHXSUCC:" + result);
                SharePrefUtil.getInstance(BindActivity.this).setObject(SharePrefUtil.HASBINDDEVICEID, device);
                //ToastUtil.displayShortToast("绑定设备成功");
                // TODO: 2017/8/22 此处还记得建立im的好友关系呢 只有建立了关系 才算是真正的绑定完 毕竟 im也是很重要的功能模块啊
                ContactManager.sendInvitationRequest(device, Constant.IM_CHILD_KEY, "hello", new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        if (0 == responseCode) {
                            //好友请求请求发送成功

                            final CommonWhiteDialog dialog = new CommonWhiteDialog(BindActivity.this);
                            dialog.setTitle("绑定成功");
                            //View view1=getLayoutInflater(R.layout.layout_item_dialog);
                            //dialog.setMiddleView(view1);
                            dialog.setContentText("欢迎加入37度妈妈大家庭，您的宝宝将与全国3547万宝宝一起在您和37度妈妈的关怀下健康成长");
                            dialog.setDisplayMiddleEnable(true);
                            dialog.setOnClickConfirmListener("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    initToPlayer();
                                    dialog.dismiss();

                                }
                            });
                            dialog.show();
                        } else {
                            //好友请求发送失败
                            LogUtils.i("XHX", "XHX 发送好友请求失败");
                        }
                    }
                });


            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);


            }
        });
    }

    private void initToPlayer() {
        Intent intent = new Intent(BindActivity.this, SettingBabyInfoActivity.class);
        intent.putExtra("isFirst", true);
        startActivity(intent);
        finish();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgRes(R.color.module_video_bg);
        toolbar.setTitle(getString(R.string.bind_drive_txt));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01 && resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            if (TextUtils.isEmpty(result)) {
                ToastUtil.displayShortToast(R.string.please_driver_valid_txt);

                return;
            }
            loadData(result);

        }
    }
}
